<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class CodeNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    protected $user;
    public function __construct($send=null)
    {
        $this->user = $send;
    }

    /**
     * Get the notification's delivery channels.
     *在这里定义通知类型,这里是邮件通知
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        //生成4个随机数字
//        $code = implode(array_random(range(1,9),4));
        $code = random_int(1000,9999);
        //验证码发送成功之后,将接收验证码的邮箱地址和发送的验证码保存到SESSION中,方便后面控制器中判断的时候使用
        //之所以要用session是因为session存放在服务器上,每一个用户的session都是唯一的
        session(['register'=>['code'=>$code,'email'=>$this->user['email']]]);
        //其实验证码不就是一个字符串吗?别把它看得多么神奇,能给邮箱发送字符串就能发送验证码,这就是验证码的本质
        return (new MailMessage)
                    ->line('你的验证码是'.$code)
                    ->action('Notification Action', url('/'));
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}
